
var netEngine = new Object();

//对服务器发来的消息的处理
netEngine.onmessage = function(event) {
    var data = JSON.parse(event.data);

    switch(data.type) {
        case 'createRoom'://创建房间
            Global.isRand = false;
            Global.myRole = data.role;
            Global.roomId = data.roomId;
            cc.director.loadScene('game');
            break;
        case 'joinRoom'://加入房间
            Global.isRand = false;
            Global.myRole = data.role;
            Global.roomId = data.roomId;
            cc.director.loadScene('game');
            break;
        case 'challengerLeaveRoom'://另一名玩家退出 不能开始游戏
            Global.gameover = true;
            Global.isJoin = false;
            cc.director.loadScene('game');
            break;
        case 'challengerJoinRoom'://另一名玩家已进入 可以开始游戏
            Global.playerChallenger.node.active = true;
            Global.isJoin = false;
            clearInterval(Global.login.showNoticeClear);
            clearInterval(Global.game.showNoticeClear);
            Global.game.noticeLabel.node.active = false;
            break;
        case 'gameStart'://游戏开始
            if (Global.gameover == false) return;
            Global.gameover = false;
            Global.game.readyCountDown();
            break;
        case 'buildStar': //生成星星
            Global.game.spawnNewStar(data.x,data.y);
            if (data.challengerGainScore == '1') {
                Global.game.challengerGainScore(1);
            }
            Global.starid = data.starid;
            break;
        case 'positionChange':
            Global.playerChallenger.node.setPosition(data.x, data.y);
            break;
        case 'accChange':
            if (data.left == 1) {
                Global.playerChallenger.accLeft = true;
                Global.playerChallenger.accRight = false;
            } else {
                Global.playerChallenger.accLeft = false;
                Global.playerChallenger.accRight = true;
            }
            break;
        case 'gameOver': //游戏结束
            Global.game.gameOver();
            break;
        case 'randCreate' ://加入匹配模式成功 等待对手 显示匹配中的字样
            Global.isRand = true;
            Global.myRole = data.role;
            Global.isJoin = false;
            cc.director.loadScene('game');
            break;
        case 'randJoin' ://加入匹配模式成功 已有对手 显示匹配中的字样
            Global.isRand = true;
            Global.myRole = data.role;
            Global.isJoin = true;
            cc.director.loadScene('game');
            //随机模式下 马上开始游戏 进入倒计时
            break;
        case 'error':
            alert(data.msg);
            Global.close();
        default:
            break;
    }    
}

module.exports = netEngine;